package com.my_musc_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my_musc_service.dao.SongListUserDao;
import com.my_musc_service.entity.SongListUser;
import com.my_musc_service.entity.SongListUserVO;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.service.SongListUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SongListUser)表服务实现类
 *
 * @author makejava
 * @since 2021-06-16 21:10:20
 */
@Service("songListUserService")
public class SongListUserServiceImpl extends ServiceImpl<SongListUserDao, SongListUser> implements SongListUserService {
    @Resource
    private SongListUserDao songListUserDao;

    @Resource
    private SongListUserService songListUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SongListUser queryById(Integer id) {
        return this.songListUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SongListUser> queryAllByLimit(int offset, int limit) {
        return this.songListUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param songListUser 实例对象
     * @return 实例对象
     */
    @Override
    public SongListUser insert(SongListUser songListUser) {
        List<SongListUser> songListUserList = songListUserDao.queryAll(songListUser);
        if (songListUserList.size() > 0) {
            throw new CustomException("当前歌单已存在此歌曲");
        }
        //获取当前歌曲的index
        SongListUser songListUserTemp = new SongListUser();
        BeanUtils.copyProperties(songListUser, songListUserTemp);
        songListUserTemp.setId(null);
        songListUserTemp.setSongId(null);
        List<SongListUser> songListUserListTemp = songListUserDao.queryAll(songListUserTemp);
        int size = songListUserListTemp.size();
        Integer songIndex = size;
        songListUser.setSongIndex(songIndex);

        this.songListUserDao.insert(songListUser);
        return songListUser;
    }

    /**
     * 修改数据
     *
     * @param songListUser 实例对象
     * @return 实例对象
     */
    @Override
    public SongListUser update(SongListUser songListUser) {
        this.songListUserDao.update(songListUser);
        return this.queryById(songListUser.getId());
    }

    @Override
    public Boolean updateSwapSongIndex(SongListUserVO songListUserVO) {
        List<SongListUser> songListUserList = songListUserDao.queryAll(songListUserVO);
        Integer from = songListUserVO.getFrom();
        Integer to = songListUserVO.getTo();
        songListUserList.sort(Comparator.comparing(SongListUser::getSongIndex));
        SongListUser songListUser = songListUserList.get(from);
        SongListUser songListUserNew = new SongListUser();
        BeanUtils.copyProperties(songListUser, songListUserNew);
        songListUserList.remove(songListUser);
        songListUserList.add(to, songListUserNew);
        for (int i = 0; i < songListUserList.size(); i++) {
            songListUserList.get(i).setSongIndex(i);
        }
        boolean b = songListUserService.updateBatchById(songListUserList);
        return b;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.songListUserDao.deleteById(id) > 0;
    }

    @Override
    public List<SongListUser> queryAll(SongListUser songListUser) {
        return this.songListUserDao.queryAll(songListUser);
    }

    @Override
    public Integer deleteBySongListUser(SongListUser songListUser) {
        return this.songListUserDao.deleteBySongListUser(songListUser);
    }

    @Override
    public Integer deleteBySongListUserBatch(List<SongListUser> songListUserList) {
        Integer rows = 0;
        if (!CollectionUtils.isEmpty(songListUserList)) {
            List<SongListUser> songListUserFromDataList = songListUserList.stream().map(songListUser -> baseMapper.selectOne(new QueryWrapper<>(songListUser)))
                    .collect(Collectors.toList());
            List<Integer> songListUserIds = songListUserFromDataList.stream().map(SongListUser::getId).collect(Collectors.toList());
            rows = baseMapper.deleteBatchIds(songListUserIds);
        }
        return rows;
    }

    @Override
    public Integer addBatch(List<SongListUser> songListUserList) {
        Integer rows = 0;
        if (!CollectionUtils.isEmpty(songListUserList)) {
            Integer songListId = songListUserList.get(0).getSongListId();
            Integer userId = songListUserList.get(0).getUserId();
            QueryWrapper<SongListUser> songListUserQueryWrapper = new QueryWrapper<>();
            songListUserQueryWrapper.eq("song_list_id", songListId);
            songListUserQueryWrapper.eq("user_id", userId);
            List<SongListUser> songListFromDataUserList = baseMapper.selectList(songListUserQueryWrapper);
            List<Integer> dataSongIds = songListFromDataUserList.stream().map(SongListUser::getSongId).collect(Collectors.toList());
            List<SongListUser> collect = songListUserList.stream().filter(songListUser -> {
                return !dataSongIds.contains(songListUser.getSongId());
            }).collect(Collectors.toList());
            Integer startIndex = dataSongIds.size();
            if (!CollectionUtils.isEmpty(collect)) {
                for (SongListUser songListUser : collect) {
                    songListUser.setSongIndex(startIndex);
                    startIndex++;
                }
                rows = baseMapper.insertBatch(collect);
            }
        }
        return rows;
    }
}
